package com.ny.controller;

import com.ny.entity.Devices;
import com.ny.entity.Result;
import com.ny.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/devices")
public class DeviceController {
    
    @Autowired
    private DeviceService deviceService;
    
    /**
     * 获取所有设备列表
     * @return
     */
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Result findAll() {
        List<Devices> devices = deviceService.findAll();
        return Result.success(devices);
    }
    
    /**
     * 根据设备名称查询设备（精确匹配）
     * @param name 设备名称
     * @return
     */
    @RequestMapping(value = "/findByName", method = RequestMethod.GET)
    public Result findByName(String name) {
        List<Devices> devices = deviceService.findByName(name);
        return Result.success(devices);
    }
    
    /**
     * 根据设备名称模糊查询设备
     * @param name 设备名称关键词
     * @return
     */
    @RequestMapping(value = "/findByNameContaining", method = RequestMethod.GET)
    public Result findByNameContaining(@RequestParam String name) {
        List<Devices> devices = deviceService.findByNameContaining(name);
        return Result.success(devices);
    }
    
    /**
     * 根据设备名称忽略大小写模糊查询设备
     * @param name 设备名称关键词
     * @return
     */
    @RequestMapping(value = "/findByNameContainingIgnoreCase", method ={  RequestMethod.POST,RequestMethod.GET})
    public Result findByNameContainingIgnoreCase(@RequestParam String name) {
        List<Devices> devices = deviceService.findByNameContainingIgnoreCase(name);
        return Result.success(devices);
    }//http://localhost:8091/devices/findByNameContainingIgnoreCase?name=

    /**
     * 添加或更新设备信息
     * @param device 设备对象
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody Devices device) {
        Devices savedDevice = deviceService.save(device);
        return Result.success(savedDevice);
    }
    
    /**
     * 创建测试设备（用于测试模糊查询）
     * @return
     */
    @RequestMapping(value = "/createTestDevices", method = RequestMethod.POST)
    public Result createTestDevices() {
        // 创建包含"厨"字的测试设备
        Devices device1 = new Devices();
        device1.setDevice_id(1001);
        device1.setName("厨房灯");
        device1.setType("light");
        device1.setStatus("on");
        device1.setBrightness(80);
        device1.setRoom("厨房");
        device1.setInstall_time(new Date());
        deviceService.save(device1);
        
        // 创建包含"厨"字的另一个测试设备
        Devices device2 = new Devices();
        device2.setDevice_id(1002);
        device2.setName("厨房空调");
        device2.setType("ac");
        device2.setStatus("off");
        device2.setBrightness(0);
        device2.setRoom("厨房");
        device2.setInstall_time(new Date());
        deviceService.save(device2);
        
        return Result.success("测试设备创建成功");
    }
    
    /**
     * 根据设备device_id更新设备名称
     * 示例：将设备ID为2的设备名称改为"灯泡"
     * @param deviceId 设备ID
     * @param name 新的设备名称
     * @return
     */
    @RequestMapping(value = "/updateNameById", method = RequestMethod.PUT)
    public Result updateDeviceNameById(@RequestParam int deviceId, @RequestParam String name) {
        Devices updatedDevice = deviceService.updateDeviceNameById(deviceId, name);
        if (updatedDevice != null) {
            return Result.success(updatedDevice);
        } else {
            return Result.error("未找到设备ID为 " + deviceId + " 的设备");
        }
    }
    
    /**
     * 根据设备device_id删除设备
     * 示例：删除设备ID为2的设备
     * @param deviceId 设备ID
     * @return
     */
    @RequestMapping(value = "/deleteByDeviceId", method = RequestMethod.DELETE)
    public Result deleteByDeviceId(@RequestParam int deviceId) {
        boolean deleted = deviceService.deleteByDeviceId(deviceId);
        if (deleted) {
            return Result.success("设备ID为 " + deviceId + " 的设备删除成功");
        } else {
            return Result.error("未找到设备ID为 " + deviceId + " 的设备");
        }
    }
}